# ============================================================================ =====
# Replication Code for:
# "The Politics of Using AI in Policy Implementation: 
#  Evidence from a Field Experiment"
#
# Authors: Yotam Margalit and Shir Raviv
# Date: December 2025
#
# This script replicates all figures and tables in the paper and appendix
# ============================================================================ =======

# Clear ----------- 
rm(list = ls())
gc()

# Load required packages -----------

packages_required <- c(
  'stargazer', 'lmtest', 'haven', 'dplyr', 'psych', 'ivreg','AER',
  'rio', 'patchwork', 'ggplot2', 'tidyverse', 'ggeffects',
  'scales', 'broom', 'tableone', 'extrafont'
)

for (package in packages_required) {
  if (!(package %in% installed.packages())) {
    install.packages(package)
  }
}
lapply(packages_required, library, character.only = TRUE)



# Set working directory to replication folder -----------

# setwd("insert here.. ")

# Load data  -----------
df <- readRDS("data.rds")

# Generating key outcome variables ---------
var_list <- list(
  
  # Wave 1 (Baseline, extended 8 outcome items)
  wave1_policy_8items = c("out1_parole", "out1_foodst", "out1_patrol", "out1_streetlighting",
                          "out1_visa", "out1_restraining", "out1_illegalbuild", "out1_shelters"),
  
  # Wave 1 (Baseline, matching Wave 2's 4 items only)
  wave1_policy_4items = c("out1_patrol", "out1_parole", "out1_foodst", "out1_streetlighting"),
  
  # Wave 2 (Immediate post-treatment, 4 outcome items)
  wave2_policy_4items = c("out2_patrol", "out2_parole", "out2_foodst", "out2_streetlighting"),
  
  # Wave 3 (Post-treatment, extended 8 outcome items)
  wave3_policy_8items = c("out3_parole", "out3_foodst", "out3_patrol", "out3_streetlighting",
                          "out3_visa", "out3_restraining", "out3_illegalbuild", "out3_shelters"),
  
  
  # Wave 3 (Post-treatment, matching Wave 2's 4 items only)
  wave3_policy_4items = c("out3_parole", "out3_foodst", "out3_patrol", "out3_streetlighting")
)



factor_list <- list(
  wave2_4items = "factorA_out2_4items",
  wave1_4items = "factorA_out1_4items",
  wave3_8items = "factorA_out3_8items",
  wave1_8items = "factorA_out1_8items",
  wave3_4items = "factorA_out3_4items"
)

for (i in seq_along(var_list)) {
  var_subset <- df[, var_list[[i]]]
  factor_fit <- fa(r = var_subset, nfactors = 1)
  loadings <- factor_fit$loadings
  
  # Calculate factor scores and re-scale
  factor_scores <- factor.scores(f = factor_fit, x = var_subset)$scores[, 1]
  rescaled_scores <- (factor_scores - min(factor_scores, na.rm = TRUE)) / (max(factor_scores, na.rm = TRUE) - min(factor_scores, na.rm = TRUE))
  
  df[, factor_list[[i]]] <- rescaled_scores
}


# Analysis samples -----------
# Sub-sample: Workers who completed Waves 1 and 2 (used for Table SI-10)
df2waves<-df%>%
  filter(!is.na(out2_placebo3)) # 1,796

# Final sample includes all workers who completed all three waves
df<-df%>%
  filter(completed==1) # 1,513


# ============================================================================ ========
# MAIN TEXT: FIGURES AND TABLES --------------
# ============================================================================ ========
# Figure (5). Pre-treatment Attitudes Toward AI in Policy  ---------------------- -----------

responses_long <- df %>% 
  dplyr::select(out1_parole:out1_shelters) %>% 
  pivot_longer(cols = everything(), names_to = "outcome_var", values_to = "response")

responses_long <- responses_long %>% 
  mutate(support_level = case_when(
    response %in% 1 ~ "Strongly oppose",
    response %in% 2:3 ~ "Somewhat oppose",
    response %in% 4 ~ "Indifferent",
    response %in% 5:6 ~ "Somewhat support",
    response %in% 7 ~ "Strongly support"
  ))

outcome_shares <- responses_long %>% 
  group_by(outcome_var, support_level) %>% 
  summarize(count = n()) %>% 
  mutate(percentage = count / sum(count) * 100)%>% 
  mutate(signed_share=case_when(support_level=="Strongly oppose"~0-percentage,
                            support_level=="Somewhat oppose"~0-percentage,  
                            support_level=="Indifferent"~0-percentage,  
                            support_level=="Somewhat support"~percentage,
                            support_level=="Strongly support"~percentage ))
outcome_shares


outcome_labels <- c(
  "out1_restraining" = "Restraining Orders",
  "out1_parole" = "Parole",
  "out1_visa" = "Visa",
  "out1_foodst" = "Food Stamps",
  "out1_illegalbuild" = "Enforcement",
  "out1_patrol" = "Police Patrol",
  "out1_shelters" = "Shelters",
  "out1_streetlighting" = "Street Lighting"
  
)

outcome_shares <- outcome_shares %>% 
  mutate(outcome_var = factor(outcome_var, levels = names(outcome_labels)))

outcome_shares$support_level2<-NA
outcome_shares$support_level2[outcome_shares$support_level=="Strongly support"]<-3
outcome_shares$support_level2[outcome_shares$support_level=="Somewhat support"]<-4
outcome_shares$support_level2[outcome_shares$support_level=="Somewhat oppose"]<-2
outcome_shares$support_level2[outcome_shares$support_level=="Strongly oppose"]<-1

outcome_shares$support_level2<-factor(outcome_shares$support_level2, levels=c(1,2,3,4),
                                  labels=c('Strongly oppose','Somewhat oppose',
                                           'Strongly support', 'Somewhat support' ))


fig5<-outcome_shares%>% 
  filter(support_level!= "Indifferent")%>% 
  mutate(outcome_var = factor(outcome_var, levels = names(outcome_labels)))%>% 
  ggplot() +
  aes(x = outcome_var,fill = support_level2, colour = support_level2, weight = signed_share) +
  geom_bar() +
  scale_x_discrete(labels = outcome_labels) +
  coord_flip() + labs(x = "", y = "", fill = "",  colour = "") +
  theme_minimal() + 
  scale_y_continuous(breaks = c(-75, -50, -25, 0, 25, 50, 75),
                     labels = c("75%", "50%", "25%", "0", "25%", "50%", "75%"))+
  
  theme(legend.position = "bottom",
        plot.background   = element_rect(fill = "white", colour = NA),
        strip.background = element_rect(fill = "white"),
        text=element_text(size=12)) +
  scale_fill_manual(values = c("#8C7D5C","#BDA575","#385052","#3C645C"))+
  scale_colour_manual(values = c("#8C7D5C","#BDA575","#385052","#3C645C"))+
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5)
fig5


ggsave(fig5, 
       filename = "fig5.png",
       height = 4, width = 7, device = ragg::agg_png,
       units     = "in", dpi = 600)


# Table (1). Effects of Experience on Attitudes ------------------
models_table1 <- list(
  # Column 1: ITT – Fashion Sample
  lm(factorA_out3_8items ~ ITT_DM + ITT_Experience + ITT_InfoValence + factorA_out1_8items, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  # Column 2: ITT – Full Sample
  lm(factorA_out3_8items ~ ITT_DM + ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
     data = df),
  
  # Column 3: ITT – Full Sample + Controls
  lm(factorA_out3_8items ~ ITT_DM + ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = df),
  
  # Column 4: TOT – Full Sample (IV)
  ivreg(factorA_out3_8items ~ TOT_DM_algo_req + ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items | 
          ITT_DM + ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
        data = df),
  
  # Column 5: ITT × Experience – Fashion Sample
  lm(factorA_out3_8items ~ ITT_DM * ITT_Experience + ITT_InfoValence + factorA_out1_8items, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  # Column 6: ITT × Experience – Full Sample
  lm(factorA_out3_8items ~ ITT_DM * ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
     data = df),
  
  # Column 7: ITT × Experience – Full Sample + Controls
  lm(factorA_out3_8items ~ ITT_DM * ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = df),
  
  # Column 8: TOT × Experience – Full Sample (IV)
  ivreg(factorA_out3_8items ~ TOT_DM_algo_req * TOT_Experience_neg + TOT_DM_algo_req + TOT_Experience_neg + 
          ITT_InfoValence + ITT_InfoContent + factorA_out1_8items | 
          ITT_DM + ITT_Experience + ITT_DM * ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
        data = df),
  
  # Column 9: TOT × Experience – Full Sample + Controls (IV)
  ivreg(factorA_out3_8items ~ TOT_DM_algo_req * TOT_Experience_neg + TOT_DM_algo_req + TOT_Experience_neg + 
          female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
          inattentive + suit_cataloging + Comp_task_indif + 
          ITT_InfoValence + ITT_InfoContent + factorA_out1_8items | 
          ITT_DM + ITT_Experience + ITT_DM * ITT_Experience +
          female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
          inattentive + suit_cataloging + Comp_task_indif + 
          ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
        data = df)
)

# Calculate first-stage F-statistics for IV models
first_stage_col4 <- lm(TOT_DM_algo_req ~ ITT_DM + ITT_Experience + ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
                       data = df)
fstat_col4 <- summary(first_stage_col4)$fstatistic[1]

first_stage_col8 <- lm(I(TOT_DM_algo_req * TOT_Experience_neg) ~ ITT_DM + ITT_Experience + ITT_DM * ITT_Experience + 
                         ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
                       data = df)
fstat_col8 <- summary(first_stage_col8)$fstatistic[1]

first_stage_col9 <- lm(I(TOT_DM_algo_req * TOT_Experience_neg) ~ ITT_DM + ITT_Experience + ITT_DM * ITT_Experience + 
                         ITT_InfoValence + ITT_InfoContent + female + age_cat3 + white + low_edu_somecol + libcons3 + 
                         tech_lit_3cat + trust_tech + HITpercent + inattentive + suit_cataloging + Comp_task_indif + 
                         factorA_out1_8items, 
                       data = df)
fstat_col9 <- summary(first_stage_col9)$fstatistic[1]

# stargazer
stargazer(models_table1,
          type = "text",
          title = "Effects of Experience on Attitudes",
          dep.var.labels = "Factor Analysis Score - Wave 3",
          column.labels = c("(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)", "(9)"),
          model.names = FALSE,
          model.numbers = FALSE,
          
          keep = c("ITT_DM", "TOT_DM_algo_req", "ITT_Experience", "TOT_Experience_neg", 
                   "factorA_out1_8items", "Constant",
                   "ITT_DM.*:.*ITT_Experience", "TOT_DM_algo_req.*:.*TOT_Experience_neg"),
          
          order = c("^ITT_DMAlgorithmic DM$", "^TOT_DM_algo_req$",
                    "ITT_DM.*:.*ITT_Experience", "TOT_DM_algo_req.*:.*TOT_Experience",
                    "ITT_Experience", "TOT_Experience_neg",
                    "factorA_out1_8items", "Constant"),
          
          covariate.labels = c(
            "Algorithmic DM",
            "Algorithmic DM (TOT)",
            "Algorithmic X Negative Exp",
            "Algorithmic X Negative Exp (TOT)",
            "Negative experience",
            "Negative experience (TOT)",
            "Pretreatment outcome",
            "Constant"
          ),
          
          add.lines = list(
            c("Demographic Controls", "No", "No", "Yes", "No", "No", "No", "Yes", "No", "Yes"),
            c("Sample", "Fashion", "Full", "Full", "Full", "Fashion", "Full", "Full", "Full", "Full"),
            c("F-test (first stage)", "–", "–", "–", 
              paste0(sprintf("%.0f", fstat_col4)),
              "–", "–", "–",
              paste0(sprintf("%.0f", fstat_col8)),
              paste0(sprintf("%.0f", fstat_col9)))
          ),
          
          omit.stat = c("adj.rsq", "ser", "f"),
          
          star.cutoffs = c(0.05, 0.01),
          star.char = c("*", "**"),
          notes.append = FALSE,
          notes.align = "l",
          digits = 3,
          font.size = "small",
          no.space = TRUE)
# Figure (6). Effects of Experience on Behaviors --------------- ------

behavior_outcomes <- c( "BHVeffort_time_maintask_median",
                        "BHVeffort_time_followup_median",
                        "BHVeffort_clicks_maintask_higher_median",
                        "BHVwellbeing_highsatis_ass",
                        "BHVperform_correct_opp_4comments",  "BHVcommit_lowerpay_highcommit")

pred_list <- list()
coef_list <- list()

for (i in 1:length(behavior_outcomes)) {
  
  lm_fullBHV <- lm(as.formula(paste(behavior_outcomes[i], "~ ITT_DM + ITT_Experience + ITT_DM * ITT_Experience + ITT_InfoContent + ITT_InfoValence")),
                   data = df)
  
  pred_lm_fullBHV <- ggpredict(lm_fullBHV, terms = c("ITT_DM"))
  
  pred_lm_fullBHV_df <- as.data.frame(pred_lm_fullBHV)
  coef_lm_fullBHV_df<-tidy(lm_fullBHV)
  
  pred_lm_fullBHV_df$outcome_var_id <- behavior_outcomes[i]
  coef_lm_fullBHV_df$outcome_var_id <- behavior_outcomes[i]
  
  pred_list[[i]] <- pred_lm_fullBHV_df
  coef_list[[i]] <- coef_lm_fullBHV_df
}

predictions_all <- do.call(rbind, pred_list)
coefficients_all <- do.call(rbind, coef_list)
predictions_all <- predictions_all %>%
  mutate(outcome = case_when(
    outcome_var_id == "BHVperform_correct_opp_4comments" ~ "Correct Classification",
    outcome_var_id == "BHVcommit_lowerpay_highcommit" ~ "Accepts Lower Pay",
    outcome_var_id == "BHVwellbeing_highsatis_ass" ~ "Assignment Satisfaction",
    outcome_var_id == "BHVeffort_time_maintask_median" ~ "Main Task Time",
    outcome_var_id == "BHVeffort_time_followup_median" ~ "Follow-up Time",
    outcome_var_id == "BHVeffort_clicks_maintask_higher_median" ~ "Clicks Count" ))
predictions_all$outcome <- factor(predictions_all$outcome,
                                  levels = c( 
                                    'Accepts Lower Pay',
                                    'Main Task Time','Follow-up Time',
                                    'Clicks Count',
                                    'Assignment Satisfaction',
                                    'Correct Classification'))
Fig6 <- predictions_all %>%
  ggplot() + aes(y = predicted, x = group, colour = x, fill = x, shape = x) +
  geom_errorbar(aes(ymin = predicted - std.error * 1.96, ymax = predicted + std.error * 1.96),
                alpha = 0.4, width = 0, size = 0.8, position = position_dodge(width = 0.29)) +
  geom_errorbar(aes(ymin = predicted - std.error * 1.64, ymax = predicted + std.error * 1.64),
                width = 0, size = 0.8, position = position_dodge(width = 0.29)) +
  geom_line(linetype = 2, alpha = 0.5, 
            position = position_dodge(width = 0.29), size = 0.5) +
  labs(x = "", y = "", colour = "", fill = "", shape = "", group = "") +
  geom_point(size = 3, position = position_dodge(width = 0.29)) +
  scale_fill_manual(values = c("#9DC4B0", "#0C7A78")) +
  scale_colour_manual(values = c("#9DC4B0", "#0C7A78")) +
  facet_wrap(~outcome, nrow = 2, scale = "free") +
  geom_text(aes(label = paste(formatC(predicted, format = "f", digits = 2, flag = "#"), 
                              "(", formatC(std.error, format = "f", digits = 2, flag = "#"), ")", sep = "")), 
            position = position_dodge(width = 0.3), size = 3, color = "#969696",
            hjust = 1.15, vjust = .65, check_overlap = TRUE)+theme_bw() + 
  theme(legend.position = "bottom", strip.background = element_rect(fill = "white"),
        panel.grid.major.x= element_blank(),
        legend.margin = margin(t = -10, r = 0, b = 15, l = 0),  
        panel.grid.major.y= element_blank(),
        plot.margin = unit(c(0, 0, 0.0, 0), "cm"), panel.grid.minor.y= element_blank(),
        text=element_text(size=12), axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()) + 
  scale_y_continuous(labels = NULL, n.breaks = 4) + scale_x_discrete(labels = NULL)
Fig6



ggsave(Fig6, 
       filename = "fig6.png",
       height = 4, width = 8, device = ragg::agg_png,
       units     = "in", dpi = 600)

# Table (2). Effects of Information on Attitudes -------

# Collect all models into a list
models_table2 <- list(
  # Column 1: Human DM Only – Minimal Model
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_Experience + factorA_out1_8items, 
     data = subset(df, ITT_DM == "Human DM")),
  
  # Column 2: Human DM Only – Socio-demographic Controls
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech,
     data = subset(df, ITT_DM == "Human DM")),
  
  # Column 3: Human DM Only – MTurk HITs and Attentiveness 
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech +
       HITpercent + inattentive, 
     data = subset(df, ITT_DM == "Human DM")),
  
  # Column 4: Full Sample – Minimal Model
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_DM * ITT_Experience + factorA_out1_8items, 
     data = df),
  
  # Column 5: Full Sample – Socio-demographic Controls
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_DM * ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech,
     data = df),
  
  # Column 6: Full Sample – MTurk HITs and Attentiveness
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + ITT_DM * ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech +
       HITpercent + inattentive, 
     data = df)
)

# stargazer 
stargazer(models_table2,
          type = "text",
          title = "Effects of Information on Attitudes",
          dep.var.labels = "Factor Analysis Score - Wave 3",
          column.labels = c("Human DM Only", "Full Sample"),
          column.separate = c(3, 3),
          model.names = FALSE,
          model.numbers = FALSE,
          keep = c("ITT_InfoContent.*:.*ITT_InfoValence_positive", 
                   "ITT_InfoContentAI$", 
                   "ITT_InfoValence_positive1$",
                   "factorA_out1_8items", 
                   "Constant"),
          order = c("ITT_InfoContent.*:.*ITT_InfoValence_positive",
                    "ITT_InfoContentAI$", 
                    "ITT_InfoValence_positive1$",
                    "factorA_out1_8items", 
                    "Constant"),
          covariate.labels = c(
            "AI X Positive info",
            "Info about AI (ref: Fashion)",
            "Positive info (ref: Negative)",
            "Pre-dispositions (wave 1)",
            "Constant"
          ),
          
          add.lines = list(
            c("Model", "Minimal", "Socio-demog", "Mturk HITs", 
              "Minimal", "Socio-demog", "Mturk HITs"),
            c("Observations", sapply(models_table2, function(x) format(nobs(x), big.mark = ","))),
            c("R²", sapply(models_table2, function(x) sprintf("%.3f", summary(x)$r.squared)))
          ),
          
          omit.stat = c("adj.rsq", "ser", "f"),
          
          star.cutoffs = c(0.05, 0.01),
          star.char = c("*", "**"),
          notes.append = FALSE,
          notes.align = "l",
          digits = 3,
          font.size = "small",
          no.space = TRUE)
# Figure (7). Effects of Exposure to Information Treatments ------


lm3_info3_fashion<-lm(factorA_out3_8items~ITT_INFOVAL3+ITT_InfoValence+ ITT_Experience+
                        female+age_cat3+white+low_edu_somecol+pid_fac+tech_lit_3cat+trust_tech+
                        HITpercent+inattentive, 
                      data = subset(df, ITT_DM=="Human DM")) 
coef_lm3_con_fashion<-tidy(lm3_info3_fashion)
ggpredict(lm3_info3_fashion, "pid_fac")

coef_lm3_con_fashion_plot <- coef_lm3_con_fashion %>% filter(term %in% c("white",
                                                                         "low_edu_somecol", "pid_facRepublican", 
                                                                         "tech_lit_3catHigh Literacy", "trust_techHigh Trust",
                                                                         "ITT_INFOVAL3AI positive", "ITT_INFOVAL3AI negative")) 
coef_lm3_con_fashion_plot$term<-factor(coef_lm3_con_fashion_plot$term, levels=c("white",
                                                                                "low_edu_somecol", 
                                                                                "pid_facRepublican",
                                                                                "tech_lit_3catHigh Literacy", 
                                                                                "trust_techHigh Trust",
                                                                                "ITT_INFOVAL3AI positive", 
                                                                                "ITT_INFOVAL3AI negative"),
                                       labels=c(
                                         "White",
                                         "No college",
                                         "Republican",  
                                         "Tech literate",
                                         "Trust in Tech",
                                         "AI info: Positive",  
                                         "AI info: Negative"))

coef_lm3_con_fashion_plot$term_colours<-NA
coef_lm3_con_fashion_plot$term_colours[coef_lm3_con_fashion_plot$term=="AI info: Positive"]<-1
coef_lm3_con_fashion_plot$term_colours[coef_lm3_con_fashion_plot$term=="AI info: Negative"]<-2
coef_lm3_con_fashion_plot$term_colours[coef_lm3_con_fashion_plot$term!="AI info: Positive" &
                                         coef_lm3_con_fashion_plot$term!="AI info: Negative" ]<-3
coef_lm3_con_fashion_plot$term_colours<-factor(coef_lm3_con_fashion_plot$term_colours)
coef_lm3_con_fashion_plot$facet<-"Factor Analysis Score"


Fig7a <- coef_lm3_con_fashion_plot %>%
  ggplot() +  aes(term, estimate, fill = term_colours, colour = term_colours) +
  geom_hline(yintercept = 0, linetype = 2, lwd = 0.9, size = 1, colour = "#ACBBBF", alpha = 1) +
  geom_errorbar(stat = "identity", alpha = 0.5, position = position_dodge(width = 0.45),
                aes(ymin = estimate - 1.96 * std.error, ymax = estimate + 1.96 * std.error), alpha = 0.01, lwd = 0.5, width = 0) +
  geom_errorbar(stat = "identity", alpha = 1, position = position_dodge(width = 0.45),
                aes(ymin = estimate - 1.64 * std.error, ymax = estimate + 1.64 * std.error), lwd = 0.8, width = 0) +
  geom_point(stat = "identity", alpha = 0.9,  size = 2, position = position_dodge(width = 0.45)) + 
  coord_flip() + 
  scale_fill_manual(values = c("#9DC4B0", "#1C6664","#548575")) +
  scale_colour_manual(values = c("#9DC4B0", "#1C6664", "#548575")) +
  labs(x = "", y = " ", title = "", colour = "", fill = "", shape = "", group = "") +
  theme_bw() +
  facet_wrap(~facet)+
  theme(legend.position = "none",  strip.background = element_rect(fill = "white"),
        strip.text = element_text(size = 12), 
        legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
        axis.text = element_text(size = 12), 
        plot.margin = unit(c(0, 0, 0.0, 0), "cm"),
        legend.text = element_text(size = 12)
  )


# Coefplot (a)
outcomes_wave3 <- c("out3_parole", "out3_foodst", "out3_patrol", "out3_streetlighting", 
                    "out3_visa", "out3_restraining", "out3_illegalbuild", "out3_shelters")
outcomes_wave1 <- c( "out1_parole", "out1_foodst", "out1_patrol", "out1_streetlighting", 
                     "out1_visa", "out1_restraining", "out1_illegalbuild", "out1_shelters")
results_df_3 <- data.frame(Model = character(),
                           Outcome = character(),
                           Coefficient = numeric(),
                           SE = numeric(),
                           p = numeric(),
                           stringsAsFactors = FALSE)
results_df_3

for (i in 1:length(outcomes_wave3)) {
  
  # Run the two models for each outcome
  lm_human_dm3 <- lm(as.formula(paste(outcomes_wave3[i], "~ ITT_INFOVAL3 + ITT_Experience +ITT_InfoValence+",
                                      outcomes_wave1[i], "+ female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + trust_tech+ HITpercent+inattentive")),
                     data = subset(df, ITT_DM == "Human DM"))
  
  # Extract coefficients and standard errors from the models and store in a data frame
  model_coef_se_df3 <- bind_rows(tidy(lm_human_dm3), .id = "model")
  model_coef_se_df3$Outcome <- outcomes_wave3[i]
  
  # Compute p-values
  model_coef_se_df3$p <- formatC(pt(abs(model_coef_se_df3$estimate/model_coef_se_df3$std.error), 
                                    df.residual(lm_human_dm3), lower.tail = FALSE, log.p = FALSE),
                                 format = "e", digits = 2)
  
  # Combine the data frames
  results_df_3 <- rbind(results_df_3, model_coef_se_df3)
}


# Coefplot (b)
results_df_3_selected<-results_df_3%>%
  filter(term=="ITT_INFOVAL3AI positive"| term=="ITT_INFOVAL3AI negative")

results_df_3_selected$term<-factor(results_df_3_selected$term, levels=c( "ITT_INFOVAL3AI negative",
                                                                         "ITT_INFOVAL3AI positive"),
                                   labels=c("AI info: Negative", "AI info: Positive"))
results_df_3_selected <- results_df_3_selected %>%
  mutate(Outcome2 = case_when(
    Outcome == "out3_parole" ~ "Parole",
    Outcome == "out3_foodst" ~ "Food stamps",
    Outcome == "out3_streetlighting" ~ "Street lighting",
    Outcome == "out3_patrol" ~ "Patrols",
    Outcome == "out3_restraining" ~ "Restraining order",
    Outcome == "out3_visa" ~ "Visa applications",
    Outcome == "out3_shelters" ~ "Shelters for homeless ",
    Outcome == "out3_illegalbuild" ~ "Increase enforcement"
  ))

Fig7b <- results_df_3_selected %>%
  mutate(Outcome3 = fct_reorder(Outcome2, desc(estimate))) %>%
  ggplot() +  aes(Outcome3, estimate, fill = term, colour = term, label = round(estimate, digits = 2)) +
  geom_hline(yintercept = 0, linetype = 2, lwd = 0.9, size = 1, colour = "#ACBBBF", alpha = 1) +
  geom_errorbar(stat = "identity", alpha = 0.5, position = position_dodge(width = 0.45),
                aes(ymin = estimate - 1.96 * std.error, ymax = estimate + 1.96 * std.error), alpha = 0.01, lwd = 0.4, width = 0) +
  geom_errorbar(stat = "identity", alpha = 1, position = position_dodge(width = 0.45),
                aes(ymin = estimate - 1.64 * std.error, ymax = estimate + 1.64 * std.error), lwd = 0.8, width = 0) +
  geom_point(stat = "identity", alpha = 0.9,  size = 2, position = position_dodge(width = 0.45)) + 
  coord_flip() + 
  scale_fill_manual(values = c("#1C6664","#9DC4B0")) +
  scale_colour_manual(values =c("#1C6664","#9DC4B0")) +
  labs(x = "", y = " ", title = "", colour = "", fill = "", shape = "", group = "") +
  facet_wrap(~Outcome2, scale = "free_y", ncol=2) +
  theme_bw() + theme(legend.position = "bottom",
                     strip.background = element_rect(fill = "white"),
                     axis.text = element_text(size = 12), axis.ticks.y = element_blank(),
                     legend.direction = "vertical" ,
                     legend.text = element_text(size = 12),
                     strip.text = element_text(size = 12),
                     legend.margin = margin(t = -10, r = 0, b = -3, l = 0),
                     legend.box.margin = margin(t = -15, r = 0, b = -3, l = 0),
                     plot.margin = unit(c(0, 0, 0, 0), "cm"),
                     axis.text.y = element_blank())


Fig7 <- Fig7a + Fig7b + plot_layout(widths = c(0.3, 0.65))


ggsave(Fig7, 
       filename = "fig7.png",
       height = 5, width = 11, device = ragg::agg_png,
       units     = "in", dpi = 600)



# Figure (8). Most Persuasive Comments among Workers Exposed to Information about AI -------

df_open<-df%>%
  filter(ITT_InfoContent=="AI")%>%
  dplyr::select(workerId,  ITT_InfoValence, task_manip_wave2, class1_wave2:class8_oppos_wave2)


comments_dict <- list(
  AI_workplace_safety = c("workplace", "safety", "accidents"),
  AI_consistent_judgments = c("consistent", "judgments", "emotional", "considerations", "fatigue"),
  evidence_based_choices = c("rational", "choices", "evidence", "accumulated", "knowledge"),
  objective_government = c( "human", "discretion", "government", "resources", "objective", "manner"),
  effective_policies = c("policymakers", "patterns", "trends", "trend", "effective", "policies", "policy"),
  improved_decision_making = c("reliable", "humans", "irrelevant factors", "mood"),
  
  data_driven_predictions = c("massive", "amounts", "data", "predictions","predictons", "accuracy"),
  transparency_concerns = c("transparency", "frustration","affected"),
  
  algorithmic_errors = c("aggregate data", "aggregate", "decisions", "individuals", "errors", "typical","typical profile", "fit", "individual"),
  perpetuating_past_inequalities = c("historical data", "inequality", "future outcomes", "history", "future", "equality"),
  private_secrete = c("systems", "secret", "Wisconsin", "private", "company", "private company", "proprietary"),
  racial_bias_in_AI = c("race", "ethnicity", "factors", "correlate", "low-income neighborhoods", "low-income",  "income", "employment history", "discriminatory"),
  algorithmic_prejudice = c("neutral", "objective", "prejudices", "amplify", "existing prejudices"),
  responsibility_concerns = c("important", "human", "oversight", "responsible", "outcomes")
)

find_best_match <- function(response, comments_dict) {
  max_count <- 0
  best_match <- ""
  
  for (category in names(comments_dict)) {
    count <- sum(str_count(tolower(response), paste(comments_dict[[category]], collapse = "|")))
    if (count > max_count) {
      max_count <- count
      best_match <- category
    }
  }
  
  return(best_match)
}
df_open <- df_open %>%
  mutate(best_match = map_chr(task_manip_wave2, find_best_match, comments_dict))

df_open$best_match<-factor(df_open$best_match)
df_open$negativetone <- 0
df_open$negativetone[ df_open$best_match=="algorithmic_errors" | df_open$best_match=="perpetuating_past_inequalities" | 
                        df_open$best_match=="private_secrete" | df_open$best_match== "racial_bias_in_AI"|
                        df_open$best_match=="algorithmic_prejudice" | df_open$best_match=="responsibility_concerns" | 
                        df_open$best_match=="data_driven_predictions" | df_open$best_match=="transparency_concerns" ]<-1
df_open$positivetone <- 0

df_open$positivetone[ df_open$best_match=="evidence_based_choices" | df_open$best_match=="improved_decision_making" | 
                        df_open$best_match=="data_driven_predictions" | df_open$best_match== "AI_workplace_safety"|
                        df_open$best_match=="AI_consistent_judgments" | df_open$best_match=="objective_government" | 
                        df_open$best_match=="effective_policies" | df_open$best_match=="transparency_concerns" ]<-1


label_map <- c(
  "AI_consistent_judgments" = "Judgment Consistency",
  "AI_workplace_safety" = "Workplace Safety",
  "algorithmic_errors" = "Algorithmic Errors",
  "algorithmic_prejudice" = "Algorithmic Prejudice",
  "data_driven_predictions" = "More Accurate Predictions",
  "effective_policies" = "Effective Policies",
  "evidence_based_choices" = "Evidence-Based Choices",
  "improved_decision_making" = "Reliability vs. Humans",
  "objective_government" = "Objective Government",
  "perpetuating_past_inequalities" = "Perpetuating Inequalities",
  "private_secrete" = "Secret Contracts",
  "racial_bias_in_AI" = "Racial Bias in AI",
  "responsibility_concerns" = "Responsibility Concerns",
  "transparency_concerns" = "Transparency Concerns"
)

create_plot <- function(dataset, info_valence, tone, color, plot_title) {
  freq_table <- dataset %>%
    filter(best_match != "") %>%
    filter(get(tone) == 1) %>%
    filter(ITT_InfoValence == info_valence) %>%
    group_by(ITT_InfoValence, best_match) %>%
    summarise(count = n()) %>%
    mutate(percentage = count / sum(count) * 100) %>%
    arrange(desc(percentage))
  
  ordered_best_match <- factor(freq_table$best_match, levels = unique(freq_table$best_match))
  
  plot <- freq_table %>%
    ggplot() +
    aes(x = ordered_best_match, weight = percentage) +
    geom_bar(fill=color) +
    coord_flip() +
    theme_minimal() +
    scale_y_continuous(limits = c(0, 35)) +
    labs(x = "", y = "", title = plot_title) +
    scale_x_discrete(labels = label_map)+theme_bw() +
    theme(legend.position = "none", 
          axis.text = element_text(size = 12), 
          plot.margin = unit(c(0, 0, 0.01, 0), "cm"),
          legend.direction = "vertical",
          legend.text = element_text(size = 12),
          plot.title = element_text(size = 12, hjust = 0.5),
          text=element_text(size = 12))
  
  return(plot)
}

pos<-df_open%>%
  filter(best_match!="transparency_concerns")
neg<-df_open%>%
  filter(best_match!="data_driven_predictions")

plot_positive <- create_plot(pos, "Positive info", "positivetone", "#98ADA4", "Positive Predictions on AI")
plot_negative <- create_plot(neg, "Negative info", "negativetone", "#1C4541", "Negative Predictions on AI")

Fig8 <- plot_positive + plot_negative
Fig8


ggsave(Fig8, 
       filename = "fig8.png",
       height = 3, width = 8, device = ragg::agg_png,
       units     = "in", dpi = 600)


# Figure (9). Treatment Effects by Predispositions -------

median_factorA_out1_8items <- median(df$factorA_out1_8items, na.rm = TRUE)
df <- df %>% 
  mutate(factorA_out1_8items_below_median = ifelse(factorA_out1_8items < median_factorA_out1_8items, 1, 0)) %>%
  mutate(factorA_out1_8items_above_median = ifelse(factorA_out1_8items >= median_factorA_out1_8items, 1, 0))
df$factorA_out1_8items_below_median[is.na(df$factorA_out1_8items)]<-NA
df$factorA_out1_8items_above_median[is.na(df$factorA_out1_8items)]<-NA


demographics <- "+female+age_cat3+white+low_edu_somecol+pid_fac+tech_lit_3cat+trust_tech+HITpercent+inattentive"
pretreatment<- "factorA_out1_8items"
controls_predis <- paste(demographics, pretreatment, sep = " + ")

# Reg model
model_predispositions <- function(outcome, data, controls) {
  formula_str <- paste(outcome, "~ ITT_InfoContent * ITT_InfoValence_positive*factorA_out1_8items_below_median+factorA_out1_8items_below_median +ITT_InfoContent + ITT_InfoValence_positive+ ITT_Experience * ITT_DM", controls)
  model <- lm(as.formula(formula_str), data = data)
  return(model)
} 

lm1_info_predis_full_controls<-model_predispositions("factorA_out3_8items", df, controls_predis)
summary(lm1_info_predis_full_controls)

plot_data <- df %>%
  dplyr::select("workerId","ITT_InfoValence_positive", "ITT_InfoValence", "tech_lit_3cat","ITT_InfoContent",
                "factorA_out1_8items_below_median","ITT_Experience","ITT_DM","female","age_cat3","factorA_out3_8items",
                "low_edu_somecol", "pid_fac", "trust_tech","inattentive", "HITpercent")%>%
  na.omit() 

plot_data$factorA_out1_8items_below_median <- factor(plot_data$factorA_out1_8items_below_median, 
                                                     levels=c(1,0),
                                                     labels=c("Averse","Favorable"))



plot_data$facet<-plot_data$factorA_out1_8items_below_median
plot_data$group<-plot_data$ITT_InfoValence_positive
plot_data$group<-factor(plot_data$group, levels=c(0,1), labels=c("Negative info", "Positive info"))
plot_data$x<-plot_data$ITT_InfoContent
plot_data <- plot_data %>%
  dplyr::select(workerId, group, facet, x, factorA_out3_8items)

ggpredict_inte<-ggpredict(lm1_info_predis_full_controls, terms=c("ITT_InfoContent", 
                                                                 "ITT_InfoValence_positive", 
                                                                 "factorA_out1_8items_below_median"))
ggpredict_inte<-as.data.frame(ggpredict_inte)
ggpredict_inte$facet<-factor(ggpredict_inte$facet, levels=c(1,0),
                             labels=c("Averse","Favorable"))
ggpredict_inte$group<-factor(ggpredict_inte$group, levels=c(0,1), labels=c("Negative info", "Positive info"))

merged_data2 <- merge(ggpredict_inte, plot_data, by = c("x", "group", "facet"))


Fig9 <- ggplot() +
  geom_jitter(data = merged_data2, aes(x = x, y = factorA_out3_8items, colour=group,
                                       group=group),
              size = 0.5, width = 0.1, height = -10,  alpha=0.14) +
  
  geom_errorbar(data = ggpredict_inte, aes(x = x, y = predicted, color = group, group = group,
                                           ymin = predicted - std.error * 1.96, ymax = predicted + std.error * 1.96),
                alpha = 0.02, width = 0, size = 0.8, position = position_dodge(width = 0.15)) +
  geom_errorbar(data = ggpredict_inte, aes(x = x, y = predicted, color = group, group = group,
                                           ymin = predicted - std.error * 1.64, ymax = predicted + std.error * 1.64),
                alpha = 0.6, width = 0, size = 0.8, position = position_dodge(width = 0.15)) +
  scale_fill_manual(values = c("#004E61", "#9BA664")) +
  scale_colour_manual(values = c("#004E61", "#9BA664")) +
  geom_line(data = ggpredict_inte, aes(x = x, y = predicted, color = group, group = group),
            size = 0.3, position = position_dodge(width = 0.15)) +
  geom_point(data = ggpredict_inte, aes(x = x, y = predicted, color = group, group = group),
             size = 3, position = position_dodge(width = 0.15)) +
  facet_wrap(~facet) +  coord_cartesian(ylim = c(0.3, 0.5)) + 
  labs(x = "", y = "", colour = "", fill = "", shape = "", group = "") +
  theme_minimal() + theme(legend.position = "bottom",  
                          strip.text = element_text(size = 12), 
                          plot.background   = element_rect(fill = "white", colour = NA),
                          legend.margin = margin(t = 0, r = 0, b = 0, l = 0),
                          axis.text = element_text(size = 12), plot.margin = unit(c(0, 0, 0.0, 0), "cm"),
                          legend.text = element_text(size = 12))
Fig9


ggsave(Fig9, 
       filename = "fig9.png",
       height = 4, width = 7, device = ragg::agg_png,
       units     = "in", dpi = 600)

# ============================================================================ ========
# SI: FIGURES AND TABLES --------------

# ============================================================================ ========
# Figure (SI-2). Attitudes toward AI in public policy, pre-treatment  ---------------------- -----------

responses_long <- df %>% 
  dplyr::select(out1_parole:out1_shelters) %>% 
  pivot_longer(cols = everything(), names_to = "outcome_var", values_to = "response")

responses_long <- responses_long %>% 
  mutate(support_level = case_when(
    response %in% 1 ~ "Strongly oppose",
    response %in% 2:3 ~ "Somewhat oppose",
    response %in% 4 ~ "Indifferent",
    response %in% 5:6 ~ "Somewhat support",
    response %in% 7 ~ "Strongly support"
  ))

outcome_shares <- responses_long %>% 
  group_by(outcome_var, support_level) %>% 
  summarize(count = n()) %>% 
  mutate(percentage = count / sum(count) * 100)%>% 
  mutate(signed_share=case_when(support_level=="Strongly oppose"~0-percentage,
                                support_level=="Somewhat oppose"~0-percentage,  
                                support_level=="Indifferent"~0-percentage,  
                                support_level=="Somewhat support"~percentage,
                                support_level=="Strongly support"~percentage ))
outcome_shares


outcome_labels <- c(
  "out1_restraining" = "Restraining Orders",
  "out1_parole" = "Parole",
  "out1_visa" = "Visa",
  "out1_foodst" = "Food Stamps",
  "out1_illegalbuild" = "Enforcement",
  "out1_patrol" = "Police Patrol",
  "out1_shelters" = "Shelters",
  "out1_streetlighting" = "Street Lighting"
  
)

outcome_shares <- outcome_shares %>% 
  mutate(outcome_var = factor(outcome_var, levels = names(outcome_labels)))


outcome_shares$support_level2<-NA
outcome_shares$support_level2[outcome_shares$support_level=="Strongly support"]<-5
outcome_shares$support_level2[outcome_shares$support_level=="Somewhat support"]<-4
outcome_shares$support_level2[outcome_shares$support_level=="Indifferent"]<-3

outcome_shares$support_level2[outcome_shares$support_level=="Somewhat oppose"]<-1
outcome_shares$support_level2[outcome_shares$support_level=="Strongly oppose"]<-2

outcome_shares$support_level2<-factor(outcome_shares$support_level2, levels=c(1,2,3,4,5),
                                  labels=c('Strongly oppose','Somewhat oppose',
                                           "Indifferent",
                                           'Strongly support', 'Somewhat support' ))


FigSI1<-outcome_shares%>% 
  mutate(outcome_var = factor(outcome_var, levels = names(outcome_labels)))%>% 
  ggplot() +
  aes(x = outcome_var,fill = support_level2, colour = support_level2, weight = signed_share) +
  geom_bar() +
  scale_x_discrete(labels = outcome_labels) +
  coord_flip() + labs(x = "", y = "", fill = "",  colour = "") +
  theme_minimal() + 
  scale_y_continuous(breaks = c(-75, -50, -25, 0, 25, 50, 75),
                     labels = c("75%", "50%", "25%", "0", "25%", "50%", "75%"))+
  
  theme(legend.position = "bottom",
        strip.background = element_rect(fill = "white"),
        text=element_text(family='Charter', size=12)) +
  scale_fill_manual(values = c("#8C7D5C","#BDA575","#CFC899", "#385052","#3C645C"))+
  scale_colour_manual(values = c("#8C7D5C","#BDA575","#CFC899","#385052","#3C645C"))+
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = 0.5)


FigSI1

ggsave(FigSI1, 
       filename = "figSI1.png",
       height = 4, width = 7, device = ragg::agg_png,
       units     = "in", dpi = 600)


# Table (SI-2). Descriptive Statistics ---------

data_statistics<-df%>%
  dplyr::select(female_fac, age_cat3, white, low_edu_somecol, libcons3, 
                dummy_diglit_GPT3,pid_fac,
                tech_lit_3cat, trust_tech,
                factorA_out1_8items, out1_foodst:out1_shelters)

vars <- c("female", "age_cat3", "white", "low_edu_somecol", "libcons3", 
          "trust_tech",  "dummy_diglit_GPT3", "tech_lit_3cat", 
          "factorA_out1_8items", 
          "out1_parole", "out1_foodst", "out1_patrol", "out1_streetlighting", 
          "out1_restraining", "out1_visa", "out1_illegalbuild", "out1_shelters")

nice_labels <- c("Gender: Female", "Age Category", "Ethnicity: White", 
                 "Education Level: Some College", "Political Leaning: Liberal-Conservative", 
                 "Trust in Technology", "Digital Literacy: GPT-3 Awareness", 
                 "Technology Literacy Category", "Factor A Outcome: 8 Items", 
                 "Outcome: Parole Decisions", "Outcome: Food Stamps", 
                 "Outcome: Police Patrols", "Outcome: Street Lighting", 
                 "Outcome: Restraining Orders", "Outcome: Visa Approvals", 
                 "Outcome: Illegal Building Reports", "Outcome: Homeless Shelters")

df_labeled <- df %>%
  rename_with(~nice_labels, all_of(vars))

table <- CreateTableOne(vars = nice_labels, data = df_labeled, test = TRUE)
table

# Tables (SI-4 & SI-5). Balance Tables for Treatment Assignment -----------
df <- df %>%
  mutate(trust_tech_num = case_when(
    trust_tech == "High Trust" ~ 1,
    trust_tech == "Low Trust"  ~ 0,
    TRUE                       ~ NA_real_
  ))

# (a) Information Condition × Valence
df$group_info <- ifelse(df$ITT_InfoContent == "AI"       & df$ITT_InfoValence == "Positive info", "AI: Positive",
                        ifelse(df$ITT_InfoContent == "AI"       & df$ITT_InfoValence == "Negative info", "AI: Negative",
                               ifelse(df$ITT_InfoContent == "Fashion"  & df$ITT_InfoValence == "Positive info", "Fashion: Positive",
                                      ifelse(df$ITT_InfoContent == "Fashion"  & df$ITT_InfoValence == "Negative info", "Fashion: Negative", NA))))

df$group_info <- factor(df$group_info,
                        levels = c("AI: Positive", "AI: Negative",
                                   "Fashion: Positive", "Fashion: Negative"))

# (b) Decision Maker × Experience
df$group_dmexp <- ifelse(df$ITT_DM == "Human DM"         & df$ITT_Experience == "Positive exp", "Human DM: Positive Exp",
                         ifelse(df$ITT_DM == "Human DM"         & df$ITT_Experience == "Negative exp", "Human DM: Negative Exp",
                                ifelse(df$ITT_DM == "Algorithmic DM"   & df$ITT_Experience == "Positive exp", "Algorithmic DM: Positive Exp",
                                       ifelse(df$ITT_DM == "Algorithmic DM"   & df$ITT_Experience == "Negative exp", "Algorithmic DM: Negative Exp", NA))))

df$group_dmexp <- factor(df$group_dmexp,
                         levels = c("Human DM: Positive Exp", "Human DM: Negative Exp",
                                    "Algorithmic DM: Positive Exp", "Algorithmic DM: Negative Exp"))


data_info <- subset(df, !is.na(group_info))
data_dm   <- subset(df, !is.na(group_dmexp))

vars     <- c("female", "age_cat3", "white", "low_edu_somecol",
              "libcons3", "trust_tech_num", "tech_lit_3cat")
cat_vars <- c("age_cat3", "libcons3", "tech_lit_3cat")

# (a) Table SI-4. Balance by Information (Content × Valence) 
tab_info <- CreateTableOne(vars       = vars,
                           strata     = "group_info",
                           data       = data_info,
                           factorVars = cat_vars)

print(tab_info,
      showAllLevels = TRUE,
      test          = FALSE,
      smd           = FALSE)

# (b) Table SI-5. Balance by Decision Maker × Experience

tab_dm <- CreateTableOne(vars       = vars,
                         strata     = "group_dmexp",
                         data       = data_dm,
                         factorVars = cat_vars)

print(tab_dm,
      showAllLevels = TRUE,
      test          = FALSE,
      smd           = FALSE)


# Table (SI-5). Effects of Experience on Alternative Outcomes ------------------

# Generate alternative measure of outcome index using PCA
get_first_pca_component <- function(data, variables){
  pca <- principal(data[variables], nfactors = 1, rotate = "none")
  return(pca$scores[,1])
}

wave3_variables <- c("out3_parole", "out3_foodst", "out3_patrol", "out3_streetlighting", "out3_restraining", "out3_visa", "out3_illegalbuild", "out3_shelters")
wave1_variables <- c("out1_parole", "out1_foodst", "out1_patrol", "out1_streetlighting", "out1_restraining", "out1_visa", "out1_illegalbuild", "out1_shelters")

df$PCA_wave3_8items <- get_first_pca_component(df, wave3_variables)
df$PCA_wave1_8items <- get_first_pca_component(df, wave1_variables)

df$PCA_wave3_binary <- ifelse(df$PCA_wave3_8items > median(df$PCA_wave3_8items, na.rm = TRUE), 1, 0)
df$PCA_wave1_binary <- ifelse(df$PCA_wave1_8items > median(df$PCA_wave1_8items, na.rm = TRUE), 1, 0)

wave3_means <- df %>% 
  group_by(PCA_wave3_binary) %>% 
  summarise(across(all_of(wave3_variables), mean, na.rm = TRUE))

wave1_means <- df %>% 
  group_by(PCA_wave1_binary) %>% 
  summarise(across(all_of(wave1_variables), mean, na.rm = TRUE))

df$PCA_wave3_8items_scaled <- scales::rescale(df$PCA_wave3_8items)
df$PCA_wave1_8items_scaled <- scales::rescale(df$PCA_wave1_8items)



models_SI5 <- list(
  # Columns 1-4: Factor Analysis Score - Wave 2
  lm(factorA_out2_4items ~ ITT_DM + ITT_Experience + ITT_InfoValence + factorA_out1_8items, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(factorA_out2_4items ~ ITT_DM + ITT_Experience + ITT_InfoValence + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(factorA_out2_4items ~ ITT_DM * ITT_Experience + ITT_InfoValence + factorA_out1_8items, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(factorA_out2_4items ~ ITT_DM * ITT_Experience + ITT_InfoValence + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  # Columns 5-8: Principle Component Analysis Score - Wave 3
  lm(PCA_wave3_8items_scaled ~ ITT_DM + ITT_Experience + ITT_InfoValence + PCA_wave1_8items_scaled, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(PCA_wave3_8items_scaled ~ ITT_DM + ITT_Experience + ITT_InfoValence + PCA_wave1_8items_scaled +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(PCA_wave3_8items_scaled ~ ITT_DM * ITT_Experience + ITT_InfoValence + PCA_wave1_8items_scaled, 
     data = subset(df, ITT_InfoContent == "Fashion")),
  
  lm(PCA_wave3_8items_scaled ~ ITT_DM * ITT_Experience + ITT_InfoValence + PCA_wave1_8items_scaled +
       female + age_cat3 + white + low_edu_somecol + libcons3 + tech_lit_3cat + trust_tech + HITpercent +
       inattentive + suit_cataloging + Comp_task_indif, 
     data = subset(df, ITT_InfoContent == "Fashion"))
)

stargazer(models_SI5,
          type = "text",
          title = "Effects of Experience on Alternative Outcomes",
          
          column.labels = c("Factor Analysis Score - Wave 2", "Principle Component Analysis - Wave 3"),
          column.separate = c(4, 4),
          
          dep.var.labels = "",
          model.names = FALSE,
          model.numbers = FALSE,
          order = c("^ITT_DMAlgorithmic DM$",
                    "^ITT_ExperienceNegative exp$",
                    "ITT_DMAlgorithmic DM:ITT_ExperienceNegative exp",
                    "^ITT_InfoValenceNegative info$",
                    "^factorA_out1_8items$",
                    "^PCA_wave1_8items_scaled$",
                    "^female$",
                    "^age_cat335-45$",
                    "^age_cat345",
                    "^white$",
                    "^low_edu_somecol$",
                    "^libcons3Moderate$",
                    "^libcons3Liberal$",
                    "^tech_lit_3catMed Literacy$",
                    "^tech_lit_3catHigh Literacy$",
                    "^trust_techHigh Trust$",
                    "^HITpercent$",
                    "^inattentive$",
                    "^suit_cataloging1$",
                    "^Comp_task_indif1$",
                    "^Constant$"),
          
          covariate.labels = c(
            "Algorithm DM",
            "Negative Experience",
            "Algo DM x Neg Exp",
            "Negative Info",
            "Pretreatment (FA)",
            "Pretreatment (PCA)",
            "Female",
            "Age: 35-45",
            "Age: 45+",
            "White",
            "Education Level (Low)",
            "Moderate",
            "Liberal",
            "Tech Literacy: Medium",
            "Tech Literacy: High",
            "Tech Trust",
            "HIT percent",
            "Inattentive",
            "Suit for Cataloging",
            "Indifferent",
            "Constant"
          ),
          
          
          omit.stat = c("adj.rsq", "ser", "f"),
          star.cutoffs = c(0.05, 0.01),
          star.char = c("*", "**"),
          notes.append = FALSE,
          notes.align = "l",
          digits = 3,
          font.size = "small",
          no.space = TRUE)
# Table (SI-6). Alternative Measures of Compliance -------------------------

# Column 1: Main effect of algorithm assignment
lm1 <- ivreg(
  factorA_out3_8items ~ TOT_DM_algo_gen + ITT_Experience + 
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items |
    ITT_DM + ITT_Experience + ITT_InfoValence + 
    ITT_InfoContent + factorA_out1_8items,
  data = df
)

# First-stage F-statistic for Column 1
first_stage_lm1 <- lm(TOT_DM_algo_gen ~ ITT_DM + ITT_Experience + 
                        ITT_InfoValence + ITT_InfoContent + factorA_out1_8items, 
                      data = df)
fstat1 <- summary(first_stage_lm1)$fstatistic
f_lm1 <- fstat1[1]
p_lm1 <- pf(fstat1[1], fstat1[2], fstat1[3], lower.tail = FALSE)

# Column 2: Interaction with negative experience
lm2 <- ivreg(
  factorA_out3_8items ~ TOT_DM_algo_gen + TOT_Experience_neg + 
    TOT_DM_algo_gen:TOT_Experience_neg +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items |
    ITT_DM + ITT_Experience + ITT_DM:ITT_Experience +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items,
  data = df
)

# First-stage for interaction term
first_stage_lm2 <- lm(
  I(TOT_DM_algo_gen * TOT_Experience_neg) ~ 
    ITT_DM + ITT_Experience + ITT_DM:ITT_Experience +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items,
  data = df
)
fstat2 <- summary(first_stage_lm2)$fstatistic
f_lm2 <- fstat2[1]
p_lm2 <- pf(fstat2[1], fstat2[2], fstat2[3], lower.tail = FALSE)

# Column 3: Full specification with all demographics
lm3 <- ivreg(
  factorA_out3_8items ~ TOT_DM_algo_gen + TOT_Experience_neg + 
    TOT_DM_algo_gen:TOT_Experience_neg +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items +
    female + age_cat3 + white + low_edu_somecol + libcons3 +
    tech_lit_3cat + trust_tech + HITpercent + inattentive +
    suit_cataloging + Comp_task_indif |
    ITT_DM + ITT_Experience + ITT_DM:ITT_Experience +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items +
    female + age_cat3 + white + low_edu_somecol + libcons3 +
    tech_lit_3cat + trust_tech + HITpercent + inattentive +
    suit_cataloging + Comp_task_indif,
  data = df
)

# First-stage for interaction term with full demographics
first_stage_lm3 <- lm(
  I(TOT_DM_algo_gen * TOT_Experience_neg) ~ 
    ITT_DM + ITT_Experience + ITT_DM:ITT_Experience +
    ITT_InfoValence + ITT_InfoContent + factorA_out1_8items +
    female + age_cat3 + white + low_edu_somecol + libcons3 +
    tech_lit_3cat + trust_tech + HITpercent + inattentive +
    suit_cataloging + Comp_task_indif,
  data = df
)
fstat3 <- summary(first_stage_lm3)$fstatistic
f_lm3 <- fstat3[1]
p_lm3 <- pf(fstat3[1], fstat3[2], fstat3[3], lower.tail = FALSE)


stargazer(lm1, lm2, lm3, 
          type = "text",
          title = "Table SI-6: Alternative Measures of Compliance",
          dep.var.labels = "Factor Analysis Score - Wave 3",
          covariate.labels = c("TOT General Algorithm",
                               "TOT General Algorithm × TOT Negative Experience",
                               "ITT Negative Experience",
                               "TOT Negative Experience",
                               "Negative Info",
                               "AI Info",
                               "Pretreatment (FA)",
                               "Suit for Cataloging",
                               "Indifferent"),
          keep = c("TOT_DM_algo_gen", "TOT_DM_algo_gen:TOT_Experience_neg",
                   "ITT_Experience", "TOT_Experience_neg",
                   "ITT_InfoValence", "ITT_InfoContent", "factorA_out1_8items",
                   "suit_cataloging", "Comp_task_indif"),
          order = c("TOT_DM_algo_gen", "TOT_DM_algo_gen:TOT_Experience_neg",
                    "ITT_Experience", "TOT_Experience_neg",
                    "ITT_InfoValence", "ITT_InfoContent", "factorA_out1_8items",
                    "suit_cataloging", "Comp_task_indif"),
          add.lines = list(
            c("Demographics", "No", "No", "Yes"),
            c("First stage F-statistic", 
              sprintf("%.2f", f_lm1),
              sprintf("%.2f", f_lm2), 
              sprintf("%.2f", f_lm3))
          ),
          omit.stat = c("adj.rsq", "ser", "f"),
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "**"),
          notes = "*p<0.05; **p<0.01",
          notes.append = FALSE,
          digits = 3)


# Table (SI-7). Effects of Experience on Behaviors and Alternative measures -------------------------

all_outcomes <- c(
  "BHVeffort_time_maintask_median",      
  "BHVeffort_time_maintask",             
  "BHVeffort_time_followup_median",     
  "BHVeffort_time_followup",             
  "BHVeffort_clicks_maintask_higher_median", 
  "BHVeffort_clicks_maintask",          
  "BHVwellbeing_highsatis_ass",          
  "BHVwellbeing_satis_assign",          
  "BHVcommit_lowerpay_highcommit",        
  "BHVperform_correct_opp_4comments"
)


formula_minimal <- "~ ITT_DM + ITT_Experience + ITT_DM:ITT_Experience + 
                     ITT_InfoContent + ITT_InfoValence"

formula_controls <- "~ ITT_DM + ITT_Experience + ITT_DM:ITT_Experience + 
                      ITT_InfoContent + ITT_InfoValence +
                      female + age_cat3 + white + low_edu_somecol + libcons3 +
                      tech_lit_3cat + trust_tech + HITpercent +
                      inattentive + suit_cataloging + Comp_task_indif"


all_models <- list()

for (outcome in all_outcomes) {
  # Minimal model
  all_models[[length(all_models) + 1]] <- lm(
    as.formula(paste(outcome, formula_minimal)),
    data = df
  )
  
  # Controls model
  all_models[[length(all_models) + 1]] <- lm(
    as.formula(paste(outcome, formula_controls)),
    data = df
  )
}

# Generate table
stargazer(all_models,
          type = "text",
          title = "Effects of Experience on Behaviors and Alternative Measures",
          dep.var.labels.include = FALSE,
          column.labels = c("Main Task Time", "", "", "",
                            "Follow-up Task Time", "", "", "",
                            "Clicks Count", "", "", "",
                            "High Satisfaction", "", "", "",
                            "Accept Lower Pay", "", 
                            "Correct Classification", ""),
          covariate.labels = c(
            "Algorithmic DM",
            "Negative Experience",
            "AI Info",
            "Negative Info",
            "Female",
            "Age 35-45",
            "Age 45+",
            "White",
            "Some College or Less",
            "Moderate",
            "Liberal",
            "Tech Literacy (Medium)",
            "Tech Literacy (High)",
            "Trust in Tech",
            "MTurk Activity",
            "Inattentive",
            "Suit Cataloging",
            "Task Indifference",
            "Algo DM × Neg Exp"
          ),
          omit.stat = c("adj.rsq", "ser", "f"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
          star.char = c("†", "*", "**", "***"),
          notes.append = FALSE,
          digits = 3,
          font.size = "footnotesize",
          column.sep.width = "1pt")

# Table (SI-8). Effects of Information on Attitudes -------------------------

models_SI8 <- list(
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_8items, 
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech,
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive, 
     data = subset(df, ITT_DM == "Human DM")),
  
  
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_8items, 
     data = df),
  
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech,
     data = df),
  
  lm(factorA_out3_8items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_8items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive, 
     data = df),
  
  
  lm(PCA_wave3_8items_scaled ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + PCA_wave1_8items_scaled, 
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(PCA_wave3_8items_scaled ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + PCA_wave1_8items_scaled +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive,
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(PCA_wave3_8items_scaled ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + PCA_wave1_8items_scaled, 
     data = df),
  
  lm(PCA_wave3_8items_scaled ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + PCA_wave1_8items_scaled + 
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive,
     data = df),
  
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_4items, 
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_4items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech,
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience + factorA_out1_4items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive, 
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_4items, 
     data = df),
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_4items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech,
     data = df),
  
  lm(factorA_out2_4items ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_DM * ITT_Experience + factorA_out1_4items +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive, 
     data = df),
  
  
  # Placebo outcomes 
  lm(out2_placebo1 ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive,
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(out2_placebo2 ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive,
     data = subset(df, ITT_DM == "Human DM")),
  
  lm(out2_placebo3 ~ ITT_InfoContent * ITT_InfoValence_positive + 
       ITT_Experience +
       female + age_cat3 + white + low_edu_somecol + libcons3 + 
       tech_lit_3cat + trust_tech + HITpercent + inattentive,
     data = subset(df, ITT_DM == "Human DM"))
  
  
  
)


stargazer(models_SI8,
          type = "text",
          title = "Effects of Information on Attitudes",
          dep.var.labels.include = FALSE,
          column.labels = c("FA Score - Wave 3", "", "", "", "", "",
                            "PCA Score - Wave 3", "", "", "",
                            "FA Score - Wave 2", "", "", "", "", "",
                            "Gun checks", "Gender quotas", "Affirmative quotas"),
          order = c("ITT_InfoContentAI:ITT_InfoValence_positive1",
                    "ITT_InfoContentAI",
                    "ITT_InfoValence_positive1",
                    "factorA_out1_8items",
                    "PCA_wave1_8items_scaled",
                    "factorA_out1_4items",
                    
                    "ITT_ExperienceNegative exp",
                    "ITT_DMAlgorithmic DM",
                    "ITT_DMAlgorithmic DM ITT_ExperienceNegative exp",
                    "female",
                    "age_cat3",
                    "white",
                    "low_edu_somecol",
                    "libcons3",
                    "tech_lit_3cat",
                    "trust_techHigh Trust",
                    "HITpercent",
                    "inattentive"),
          covariate.labels = c(
            "AI X Positive info",
            "AI Info",
            "Positive Info",
            "Pretreatment FA (8 items)",
            "Pretreatment PCA (8 items)",
            "Pretreatment FA (4 items)",
            
            "Negative experience",
            "Algorithmic DM",
            "Algorithmic X Negative experience",
            "Female",
            "Age: 35-45",
            "Age: 45+",
            "White",
            "Education Level (Low)",
            "Independent",
            "Republican",
            "Tech Literacy: medium",
            "Tech Literacy: high",
            "Trust in tech",
            "MTurk intensity",
            "Inattentive"
          ),
          omit.stat = c("adj.rsq", "ser", "f"),
          
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
          star.char = c("†", "*", "**", "***"),
          notes.append = FALSE,
          digits = 3,
          font.size = "footnotesize",
          column.sep.width = "1pt")



# Table (SI-9). Full results of Figure 5 and Alternative Measures -----------

df$ITT_InfoValence <- relevel(df$ITT_InfoValence, ref = "Negative info")

vars <- c("out3_parole", "out3_foodst", "out3_patrol", "out3_streetlighting", 
          "out3_visa", "out3_restraining", "out3_illegalbuild", "out3_shelters", 
          "out1_parole", "out1_foodst", "out1_patrol", "out1_streetlighting", 
          "out1_visa", "out1_restraining", "out1_illegalbuild", "out1_shelters")

df <- df %>%
  mutate(across(all_of(vars), ~ifelse(. %in% c(6,7), 1, 0), .names = "{.col}_bin"))


lm3_info3_full <- lm(
  factorA_out3_8items ~ ITT_INFOVAL3 + ITT_Experience + ITT_DM + ITT_InfoValence +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = df
)

lm3_info3_fashion_coefplot <- lm(
  factorA_out3_8items ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)


LM_out3_patrol_hdm <- lm(
  out3_patrol ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + out1_patrol +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_parolel_hdm <- lm(
  out3_parole ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + out1_parole +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_foodst_hdm <- lm(
  out3_foodst ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + out1_foodst +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_streetlighting_hdm <- lm(
  out3_streetlighting ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + 
    out1_streetlighting + female + age_cat3 + white + low_edu_somecol + pid_fac + 
    tech_lit_3cat + trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_illegalbuild_hdm <- lm(
  out3_illegalbuild ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + 
    out1_illegalbuild + female + age_cat3 + white + low_edu_somecol + pid_fac + 
    tech_lit_3cat + trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_restraining_hdm <- lm(
  out3_restraining ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + 
    out1_restraining + female + age_cat3 + white + low_edu_somecol + pid_fac + 
    tech_lit_3cat + trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_visa_hdm <- lm(
  out3_visa ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + out1_visa +
    female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

LM_out3_shelters_hdm <- lm(
  out3_shelters ~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence + 
    out1_shelters + female + age_cat3 + white + low_edu_somecol + pid_fac + 
    tech_lit_3cat + trust_tech + HITpercent + inattentive, 
  data = subset(df, ITT_DM == "Human DM")
)

byitem_model <- function(outcome_var, pretreatment_var, data) {
  formula <- as.formula(paste(
    outcome_var, "~ ITT_INFOVAL3 + ITT_Experience + ITT_InfoValence +", 
    pretreatment_var,
    "+ female + age_cat3 + white + low_edu_somecol + pid_fac + tech_lit_3cat + 
    trust_tech + HITpercent + inattentive"
  ))
  lm(formula, data = subset(data, ITT_DM == "Human DM"))
}

LM_out3_bin_patrol_hdm <- byitem_model("out3_patrol_bin", "out1_patrol", df)
LM_out3_bin_parolel_hdm <- byitem_model("out3_parole_bin", "out1_parole", df)
LM_out3_bin_foodst_hdm <- byitem_model("out3_foodst_bin", "out1_foodst", df)
LM_out3_bin_streetlighting_hdm <- byitem_model("out3_streetlighting_bin", 
                                               "out1_streetlighting", df)
LM_out3_bin_illegalbuild_hdm <- byitem_model("out3_illegalbuild_bin", 
                                             "out1_illegalbuild", df)
LM_out3_bin_restraining_hdm <- byitem_model("out3_restraining_bin", 
                                            "out1_restraining", df)
LM_out3_bin_visa_hdm <- byitem_model("out3_visa_bin", "out1_visa", df)
LM_out3_bin_shelters_hdm <- byitem_model("out3_shelters_bin", "out1_shelters", df)

models_SI9 <- list(
  lm3_info3_full,
  lm3_info3_fashion_coefplot,
  LM_out3_patrol_hdm,
  LM_out3_parolel_hdm,
  LM_out3_foodst_hdm,
  LM_out3_streetlighting_hdm,
  LM_out3_illegalbuild_hdm,
  LM_out3_restraining_hdm,
  LM_out3_visa_hdm,
  LM_out3_shelters_hdm,
  LM_out3_bin_patrol_hdm,
  LM_out3_bin_parolel_hdm,
  LM_out3_bin_foodst_hdm,
  LM_out3_bin_streetlighting_hdm,
  LM_out3_bin_illegalbuild_hdm,
  LM_out3_bin_restraining_hdm,
  LM_out3_bin_visa_hdm,
  LM_out3_bin_shelters_hdm
)

stargazer(models_SI9,
          type = "text",
          title = "Full results of Figure 5 and Alternative Measures by individual items",
          dep.var.labels.include = FALSE,
          column.labels = c("FA score (Fig 5)", "",
                            "Policy domains", "", "", "", "", "", "", "",
                            "Policy domains (binary)", "", "", "", "", "", "", ""),
          
          order = c("ITT_INFOVAL3AI positive",
                    "ITT_INFOVAL3AI negative",
                    "ITT_InfoValencePositive info",
                    "ITT_ExperienceNegative exp",
                    "ITT_DMAlgorithmic DM",
                    "out1_patrol",
                    "out1_parole",
                    "out1_foodst",
                    "out1_streetlighting",
                    "out1_illegalbuild",
                    "out1_restraining",
                    "out1_visa",
                    "out1_shelters",
                    "female",
                    "age_cat3",
                    "white",
                    "low_edu_somecol",
                    "pid_facIndependent or Other",
                    "pid_facRepublican",
                    "tech_lit_3catMed Literacy",
                    "tech_lit_3catHigh Literacy",
                    "trust_techHigh Trust",
                    "HITpercent",
                    "inattentive"),
          covariate.labels = c(
            "AI Info: positive",
            "AI Info: negative",
            "Tone: Negative",
            "Negative experience",
            "Algorithmic DM",
            "Pretreatment: Patrol",
            "Pretreatment: Parole",
            "Pretreatment: Foodstamps",
            "Pretreatment: Streetlight",
            "Pretreatment: Enforcement",
            "Pretreatment: Restraining Order",
            "Pretreatment: Visa",
            "Pretreatment: Shelters",
            "Female",
            "Age: 35-45",
            "Age: 45+",
            "White",
            "Education Level (Low)",
            "Independent",
            "Republican",
            "Tech Literacy: medium",
            "Tech Literacy: high",
            "Trust in tech",
            "MTurk intensity",
            "Inattentive"
          ),
          omit.stat = c("adj.rsq", "ser", "f"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
          star.char = c("†", "*", "**", "***"),
          notes.append = FALSE,
          digits = 3,
          font.size = "footnotesize",
          column.sep.width = "1pt")


# Table (SI-10). Estimating bounds of the treatment effect  ------

df2waves <- df2waves %>%
  mutate(ATTop1_FCscore31 = ifelse(is.na(factorA_out3_8items), factorA_out1_8items, factorA_out3_8items),
         ATTop2_FCscore32 = ifelse(is.na(factorA_out3_8items), factorA_out2_4items, factorA_out3_8items))

models_SI10 <- list(
  lm(ATTop1_FCscore31~ITT_InfoContent*ITT_InfoValence_positive+ITT_DM*ITT_Experience+factorA_out1_8items, 
     data = df2waves),
  
  lm(ATTop1_FCscore31~ITT_InfoContent*ITT_InfoValence_positive+ITT_DM*ITT_Experience+factorA_out1_8items+ 
       female+age_cat3+white+low_edu_somecol+libcons3+tech_lit_3cat+trust_tech+ HITpercent+inattentive,
     data = df2waves),
  
  lm(ATTop1_FCscore31~ITT_InfoContent*ITT_InfoValence_positive+ ITT_Experience+factorA_out1_8items, 
     data = subset(df2waves, ITT_DM=="Human DM")),
  
  lm(ATTop1_FCscore31~ITT_InfoContent*ITT_InfoValence_positive+ ITT_Experience+factorA_out1_8items+
       female+age_cat3+white+low_edu_somecol+libcons3+tech_lit_3cat+trust_tech+ HITpercent+inattentive,
     data = subset(df2waves, ITT_DM=="Human DM")),
  
  
  
  lm(ATTop2_FCscore32~ITT_InfoContent*ITT_InfoValence_positive+ITT_DM*ITT_Experience+factorA_out1_8items, 
     data = df2waves),
  
  lm(ATTop2_FCscore32~ITT_InfoContent*ITT_InfoValence_positive+ITT_DM*ITT_Experience+factorA_out1_8items+ 
       female+age_cat3+white+low_edu_somecol+libcons3+tech_lit_3cat+trust_tech+ HITpercent+inattentive,
     data = df2waves),
  
  lm(ATTop2_FCscore32~ITT_InfoContent*ITT_InfoValence_positive+ ITT_Experience+factorA_out1_8items, 
     data = subset(df2waves, ITT_DM=="Human DM")),
  
  lm(ATTop2_FCscore32~ITT_InfoContent*ITT_InfoValence_positive+ ITT_Experience+factorA_out1_8items+
       female+age_cat3+white+low_edu_somecol+libcons3+tech_lit_3cat+trust_tech+ HITpercent+inattentive,
     data = subset(df2waves, ITT_DM=="Human DM"))) 


stargazer(models_SI10,
          type = "text",
          title = "Estimating bounds of the treatment effect",
          dep.var.labels.include = FALSE,
          order=c("ITT_InfoContentAI:ITT_InfoValence_positive1", 
                  "ITT_InfoContentAI", "ITT_InfoValence_positive",
                  "factorA_out1_8items", "ITT_DMAlgorithmic DM",
                  "ITT_ExperienceNegative exp",
                  "ITT_DMAlgorithmic DM:ITT_ExperienceNegative exp",
                  "female","age_cat335-45","age_cat345+","white","low_edu_somecol","libcons3Moderate","libcons3Liberal",
                  "tech_lit_3catMed Literacy",  "tech_lit_3catHigh Literacy", "trust_techHigh Trust", 
                  "HITpercent", "inattentive",
                  "Constant"),
          covariate.labels = c("AI X Positive info",
                               "Info about AI (ref: Fashion)",
                               "Positive info  (ref: Negative)",
                               "Pretreatment FA (8 items)",
                               "Algorithm DM","Experience Positive","Algorithm X Positive Exp",
                               "Female", "Age: 35-45", "Age: 45+",
                               "White", "Education level (Low)",
                               "Independent",  "Republican",
                               "Tech Literacy: medium", "Tech Literacy: high",
                               "Trust in tech",  "MTurk intensity",  "Inattentive",
                               "Constant"),
          column.separate = c(4,4), column.labels = c("Lower bound FA score pre-treatment outcome (wave 1)", "Upper bound FA score posttreatment outcome (wave 2)"),
          omit.stat = c("adj.rsq", "ser", "f"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("†", "*", "**", "***"),
          notes.append = FALSE,
          digits = 3)


# Table (SI-11). Effects of Information Treatments by Predispositions ---------

baseline_median <- median(df$factorA_out1_8items, na.rm = TRUE)

df <- df %>% 
  mutate(
    averse_to_ai = ifelse(factorA_out1_8items <= baseline_median, 1, 0),
    favorable_to_ai = ifelse(factorA_out1_8items >= baseline_median, 1, 0)
  )
df$averse_to_ai[is.na(df$factorA_out1_8items)] <- NA
df$favorable_to_ai[is.na(df$factorA_out1_8items)] <- NA

pct_10 <- quantile(df$factorA_out1_8items, probs = 0.1, na.rm = TRUE)
pct_90 <- quantile(df$factorA_out1_8items, probs = 0.9, na.rm = TRUE)
pct_45 <- quantile(df$factorA_out1_8items, probs = 0.45, na.rm = TRUE)
pct_55 <- quantile(df$factorA_out1_8items, probs = 0.55, na.rm = TRUE)


# Full sample
sample_full <- df

# exclude top 10% (most favorable)
sample_excl_top10 <- subset(df, factorA_out1_8items < pct_90)

# exclude bottom 10% (most averse)
sample_excl_bottom10 <- subset(df, factorA_out1_8items > pct_10)

# exclude midpoint (45th-55th percentiles)
sample_excl_midpoint <- subset(df, 
                               factorA_out1_8items < pct_45 | 
                                 factorA_out1_8items > pct_55)



formula_main <- factorA_out3_8items ~ 
  ITT_InfoContent * ITT_InfoValence_positive * averse_to_ai +
  ITT_DM + 
  ITT_Experience


model_full <- lm(formula_main, data = sample_full)
model_excl_top10 <- lm(formula_main, data = sample_excl_top10)
model_excl_bottom10 <- lm(formula_main, data = sample_excl_bottom10)
model_excl_midpoint <- lm(formula_main, data = sample_excl_midpoint)


stargazer(
  model_full,
  model_excl_top10,
  model_excl_bottom10,
  model_excl_midpoint,
  type = "text",
  
  
  order = c(
    6,   # AI x Positive Info
    9,   # AI x Positive Info x Averse
    7,   # AI x Averse
    8,   # Positive Info x Averse
    2,   # Positive info
    1,   # AI Info
    3,   # Pretreatment outcome
    4,   # Algorithmic DM
    5,   # Negative Experience
    10   # Constant
  ),
  
  # Variable labels (matching reordered sequence)
  covariate.labels = c(
    "AI x Positive Info",
    "AI x Positive Info x Averse",
    "AI x Averse",
    "Positive Info x Averse",
    "Positive info",
    "AI Info",
    " Pretreatment FA",
    "Algorithmic DM",
    "Negative Experience",
    "Constant"
  ),
  
  # Column headers
  dep.var.labels = "Dependent variable:",
  column.labels = c(
    "Full",
    "Excluding top 10%",
    "Excluding bottom 10%",
    "Excluding midpoint (45-55)"
  ),
  
  # Significance stars
  star.cutoffs = c(0.10, 0.05, 0.01, 0.001),
  star.char = c("†", "*", "**", "***"),
  notes = "†p<0.1; *p<0.05; **p<0.01; ***p<0.001",
  notes.append = FALSE,
  
  # Table formatting
  omit.stat = c("adj.rsq", "ser", "f")
)